package com.github.xiaolyuh.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 设置线程名称的ThreadFactory
 *
 * @author olafwang
 */
public class NamedThreadFactory implements ThreadFactory {

	private final AtomicInteger poolNumber = new AtomicInteger(1);

	private final ThreadGroup threadGroup;

	private final AtomicInteger threadNumber = new AtomicInteger(1);

	public final String namePrefix;

	public NamedThreadFactory(String name) {
		//SecurityManager s = System.getSecurityManager();
		//threadGroup = (s != null) ? s.getThreadGroup() :
		//        Thread.currentThread().getThreadGroup();
		threadGroup = Thread.currentThread().getThreadGroup();
		if (null == name || "".equals(name.trim())) {
			name = "pool";
		}
		namePrefix = name + "-" + poolNumber.getAndIncrement() + "-thread-";
	}

	@Override
	public Thread newThread(Runnable runnable) {
		Thread thread = new Thread(threadGroup, runnable, namePrefix + threadNumber.getAndIncrement(), 0);
		if (thread.isDaemon()) {
			thread.setDaemon(false);
		}
		if (thread.getPriority() != Thread.NORM_PRIORITY) {
			thread.setPriority(Thread.NORM_PRIORITY);
		}
		return thread;
	}
}